package com.bigData.main.controller;

import com.bigData.main.TestSocket.TestClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/api/code")
public class CodeController {

    @PostMapping("/verify")
    public ResponseEntity<?> verifyCode(@RequestBody Map<String, String> request) {
        try {
            String code = request.get("code");
            String serverIp = request.get("serverIp"); // 新增IP参数

            if (serverIp == null || serverIp.isEmpty()) {
                serverIp = "127.0.0.1"; // 默认值
            }

            String result = TestClient.verifyCode(code, serverIp); // 传递两个参数

            if ("invalid_code".equals(result)) {
                return ResponseEntity.status(401).body("对接码错误");
            }

            return ResponseEntity.ok(result);
        } catch (IOException e) {
            return ResponseEntity.status(500).body("服务器连接失败");
        }
    }
}